/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg {
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;

	/**
	 * @author Marek Brun
	 */
	public class InstanceDebugServiceProxy {
		
		private static const servicedObjects : Dictionary = new Dictionary(true);
		private static var mldbgmain:Object; //MLDBG
		private var debuggedInstance:Object;
		private var dbg : Object; //ClassDBGService
		
		public function InstanceDebugServiceProxy(access:Private, debuggedInstance:Object) {
			this.debuggedInstance=debuggedInstance;
			
		}
		
		public function log(text:*, args:Object=null, dbgContextID:*=null):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			if(args){
				dbg.logMethod(text, args, dbgContextID);
			}else{
				dbg.log(text, dbgContextID);
			}
		}
		
		public function logLocal(text:*, args:Object=null, dbgContextID:*=null):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			dbg.logLocal(text);
		}
		
		public function logv(name:*, data:*=null, dbgContextID:*=null):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			dbg.logv(name, data, dbgContextID);
		}
		
		public function link(value:*, isRemember:Boolean=false):String {
			if(!mldbgmain){ return ''; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			return dbg.link(value, isRemember);
		}
		
		public function logFPSTag(text:String):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			dbg.logFPSTag(text);
		}
		
		public function logMemoryTag(text:String):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			dbg.logMemoryTag(text);
		}
		
		protected function logStartSpeedTest(name:String):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(this); }
			dbg.startSpeedTest(name);
		}
		
		protected function logFinishSpeedTest(name:String):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(this); }
			dbg.finishSpeedTest(name);
		}
		
		public function openDBGWindow():void {
			if(!mldbgmain){ return; }
			mldbgmain.openObjectWindow(debuggedInstance);
		}
		
		public static function setupMLDBG(mldbgmainInstance:Object/*MLDBGMain*/):void {
			mldbgmain=mldbgmainInstance;
		}
		
		public function registerAtDBGContext(dbgContextID:*):void {
			if(!mldbgmain){ return; }
			if(!dbg){ dbg=mldbgmain.getDebugService(debuggedInstance); }
			dbg.registerAtDBGContext(dbgContextID);
		}
		
		public static function isDebuggerEnabled():Boolean {
			return Boolean(mldbgmain);
		}
		
		public static function getMLDBGMain():Object {
			return mldbgmain;
		}
		
		public function registerInDisplay(display:DisplayObject):void {
			if(!mldbgmain){ return; }
			mldbgmain.registerObjectInDisplay(debuggedInstance, display);
		}
		
		public static function registerObjectInDisplay(obj:Object, display:DisplayObject):void {
			mldbgmain.registerObjectInDisplay(obj, display);	
		}
		
		public static function forInstance(debuggedInstance:Object):InstanceDebugServiceProxy {
			if(servicedObjects[debuggedInstance]){
				return servicedObjects[debuggedInstance];
			}else{
				servicedObjects[debuggedInstance]=new InstanceDebugServiceProxy(null, debuggedInstance);
				return servicedObjects[debuggedInstance];
			}
		}
		
		public static function exeLink(link:String):void {
			if(!mldbgmain){ return; }
			mldbgmain.exeLink(link);
		}
	}
}
internal class Private {}
